#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128_t ML;
const int MAXN = 1e4+7;
const LL MOD = 1e9+7;
int prime[MAXN], p_cnt;
bool not_p[MAXN];
void Init() {
    p_cnt=0;
    for (int i=2; i<MAXN; ++i) {
        if (!not_p[i]) {
            prime[p_cnt++] = i;
        }
        for (int j=0; j<p_cnt&&prime[j]*i<MAXN; ++j) {
            not_p[i*prime[j]] = true;
            if (i%prime[j]==0) {
                break;
            }
        }
    }
}
int li_p[10], li_cnt;
void Fenjie(int ask) {
    li_cnt=0;
    for (int i=0; i<p_cnt&&ask!=1; ++i) {
        if (ask%prime[i]==0) {
            int t = prime[i];
            li_p[li_cnt++] = t;
            while (ask%t==0) ask /= t;
        }
    }
    if (ask!=1) li_p[li_cnt++] = ask;
}
LL SA(LL n) {
    return ((ML)n)*(n+1)*(2*n+1)/6 % MOD;
}
LL SB(LL n) {
    return n*(n+1)/2 % MOD;
}

int main() {
    Init();
    LL n, m;
    while (~scanf("%lld %lld", &n, &m)) {
        Fenjie(m);
        int len = (1<<(li_cnt));
        LL res = SA(n) + SB(n);
        res %= MOD;
        for (int i=1; i<len; ++i) {
            int One = 0;
            LL pp = 1;
            for (int j=0; j<li_cnt; ++j) {
                if (i&(1<<j)) {
                    ++ One;
                    pp *= li_p[j];
                }
            }
            if (One&1) {
                res = (res - pp*pp%MOD*SA(n/pp)%MOD - pp*SB(n/pp) % MOD) % MOD;
            }
            else {
                res = (res + pp*pp%MOD*SA(n/pp)%MOD + pp*SB(n/pp) % MOD) % MOD;
            }
        }
        if (res<0) res += MOD;
        cout<<res<<endl;
    }
    return 0;
}
